#!/usr/bin/env python
# encoding: utf-8
import json
import pandas as pd
from feature_set.base_feature import BaseFeature, RequstData
from feature_set.dev.un.dev_un_base_v2.utils.dev_v2_features import format_device_data, get_device_list_features


class DevUnBaseV2(BaseFeature):
    def __init__(self):
        super().__init__()
        self.function_map = {
            'dev2_un_features': self.get_device_v2_features
        }
        self.country_id = None

    def load_conf(self):
        """
        load config file
        Returns:

        """
        pass

    def load_request(self, request_data: RequstData):
        """
        get request data and format feature input data
        """

        apply_time = request_data.apply_time
        dev_list = request_data.data_sources['device_list']
        if dev_list == [] or dev_list == '' or dev_list is None:
            dev_df = pd.DataFrame(columns=['user_id',
                                           'update_time',
                                           'advertising_id',
                                           'and_id',
                                           'background_recovery_times',
                                           'battery_health',
                                           'battery_power',
                                           'battery_temperature',
                                           'boot_date',
                                           'boot_time',
                                           'charge_type',
                                           'client_type',
                                           'cpu_cur',
                                           'cpu_max',
                                           'cpu_min',
                                           'deviceId',
                                           'deviceBrand',
                                           'gaid',
                                           'has_gravity_sensor',
                                           'has_gyroscope_sensor',
                                           'ip',
                                           'is_airplane_mode',
                                           'is_charging',
                                           'is_mock_location',
                                           'is_root',
                                           'is_tablet',
                                           'is_usb_debug',
                                           'is_using_proxy_port',
                                           'is_using_vpn',
                                           'keyboard',
                                           'loan_page_stay_time',
                                           'locale_iso_3_country',
                                           'ram_total_size',
                                           'ram_usable_size',
                                           'read_privacy_agreement_time',
                                           'read_privacy_agreement_times',
                                           'ringer_mode',
                                           'screen_brightness',
                                           'sd_card_available_size',
                                           'sim_count'])
        else:
            dev_df = pd.DataFrame(dev_list)
            dev_df['user_id'] = 1
        try:
            self.data = format_device_data(dev_df, country_id=self.country_id, current_apply_time=apply_time)
        except:
            self.data = pd.DataFrame([],columns=[['user_id', 'update_time', 'advertising_id', 'and_id',
                   'background_recovery_times', 'battery_health', 'battery_power',
                   'battery_temperature', 'boot_date', 'boot_time', 'charge_type',
                   'client_type', 'deviceId', 'deviceBrand', 'gaid', 'has_gravity_sensor',
                   'has_gyroscope_sensor', 'ip', 'is_airplane_mode', 'is_charging',
                   'is_mock_location', 'is_root', 'is_tablet', 'is_usb_debug',
                   'is_using_proxy_port', 'is_using_vpn', 'keyboard',
                   'loan_page_stay_time', 'locale_iso_3_country', 'ram_total_size',
                   'ram_usable_size', 'read_privacy_agreement_time',
                   'read_privacy_agreement_times', 'ringer_mode', 'screen_brightness',
                   'sd_card_available_size', 'sim_count', 'sdcard_available_size',
                   'day_inter', 'boot_day_inter']])

    def get_device_v2_features(self):
        dev_features = get_device_list_features(self.data)
        return dev_features
